<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Product_Model extends CI_Model {

    protected $_lang = '';
    protected $_table = 'products';

    function __construct() {
        parent::__construct();
    }

    public function initialise($lang) {
        if ($lang != '' && $lang != 'vn')
            $this->_lang = '_' . $lang;
        else
            $this->_lang = '';
    }

    function getProductCategoryAlias($alias) {
        $this->db->where('bl_active', 1);
        $this->db->where('name_alias', $alias);
        $result = $this->db->get('product_category');
        if ($result)
            return $result->row();
        return '';
    }

    function getAlias($id) {
        $this->db->where('bl_active', 1);
        $this->db->where('product_category_id', $id);
        $result = $this->db->get('product_category')->row();
        if ($result)
            return $result->name_alias;
        return '';
    }

    function getProductCategoryId($id) {
        $this->db->where('bl_active', 1);
        $this->db->where('product_category_id', $id);
        return $result = $this->db->get('product_category')->row();
    }

    function getProductFromId($id) {
        $this->db->select('products.*,partners.name as partners_name, partners.name_alias as partners_name_alias,partners.details as partners_details');
        $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
        $this->db->where('products.bl_active', 1);
        $this->db->where('products_id', $id);
        return $result = $this->db->get('products')->row();
    }

    function getProductFromCode($id) {
        $this->db->select('products.*,partners.name as partners_name, partners.name_alias as partners_name_alias,partners.details as partners_details');
        $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
        $this->db->where('products.bl_active', 1);
        $this->db->where('code', $id);
        return $result = $this->db->get('products')->row();
    }
    function getProductFromSku($id) {
        $this->db->select('products.*,partners.name as partners_name, partners.name_alias as partners_name_alias,partners.details as partners_details');
        $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
        $this->db->where('products.bl_active', 1);
        $this->db->where('sku', $id);
        return $result = $this->db->get('products')->row();
    }
    function getProductFromPartnerAlias($alias = "") {
        if($alias != "") {
            $this->db->select('products.*,partners.name as partners_name, partners.name_alias as partners_name_alias,partners.details as partners_details');
            $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
            $this->db->where('products.bl_active', 1);
            $this->db->where("partners.name_alias", $alias);
            return $result = $this->db->get('products')->result();
        }
        return;
    }

 function getNumProductFromPartnerAlias($alias = "") {
        if($alias != "") {
            $this->db->select('products_id');
            $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
            $this->db->where('products.bl_active', 1);
            $this->db->where("partners.name_alias", $alias);
            return $result = $this->db->get('products')->num_rows();
        }
        return;
    }


    function getOtherFromCategory($product_category_id = 0, $id = 0) {
        $substring_procate = $this->getStringSubProductCategory($product_category_id);
        $arr_where_in = explode(',', $substring_procate);
        $this->db->select('products_id,code,products.image,title,products.name,products.name_alias,viewed,sprice,price,partners.name as partners_name, partners.name_alias as partners_name_alias');
        $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
        $this->db->where_in('product_category_id', $arr_where_in);
        $this->db->where('products.bl_active', 1);
        $this->db->where('products_id <> ', $id);
        $result = $this->db->get('products', 20, 0)->result();
        foreach ($result as $key => $item) {
            $salesOff = 0;
            if ($item->sprice > 0 && $item->price > 0)
                $salesOff = 100 - round($item->sprice / $item->price * 100);
            $result[$key]->salesOff = $salesOff;
        }
        return $result;
    }

    function getOtherFromCategoryRandom($product_category_id = 0, $id = 0) {
        $substring_procate = $this->getStringSubProductCategory($product_category_id);
        $arr_where_in = explode(',', $substring_procate);
        $this->db->select('products_id,code,products.image,title,products.name,products.name_alias,viewed,sprice,price,partners.name as partners_name, partners.name_alias as partners_name_alias');
        $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
        $this->db->where_in('product_category_id', $arr_where_in);
        $this->db->where('products.bl_active', 1);
        $this->db->where('products_id <> ', $id);
        $this->db->order_by('rand()');
        $result = $this->db->get('products', 20, 0)->result();
        foreach ($result as $key => $item) {
            $salesOff = 0;
            if ($item->sprice > 0 && $item->price > 0)
                $salesOff = 100 - round($item->sprice / $item->price * 100);
            $result[$key]->salesOff = $salesOff;
        }
        return $result;
    }

    function getViewedProducts() {
        $this->db->select('products_id,code,image,name,title,name_alias,viewed,sprice,price');
        $this->db->where('bl_active', 1);
        $this->db->order_by('rand()');
        return $this->db->get('products', 24, 0)->result();
    }

    function update_View($id, $view) {
        $this->db->where('products_id', $id);
        $data = array('viewed' => $view);
        $query = $this->db->update('products', $data);
        if ($query) {
            return true;
        } else {
            return false;
        }
    }

    function getProduct($condition = array(), $start = 0, $num = 20) {
        $this->db->select("{$this->_table}.*, partners.name as partners_name, partners.name_alias as partners_name_alias");
        $this->db->where("{$this->_table}.bl_active", 1);
        if (!empty($condition)) {
            foreach ($condition as $key => $value) {
                $this->db->where($key, $value);
            }
        }
        $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
        $this->db->order_by('dt_create', 'DESC');
        $result = $this->db->get($this->_table, $num, $start)->result();
        foreach ($result as $key => $item) {
            $salesOff = 0;
            if ($item->sprice > 0 && $item->price > 0)
                $salesOff = 100 - round($item->sprice / $item->price * 100);
            $result[$key]->salesOff = $salesOff;
        }
        return $result;
    }

    function getProductFromCategory($product_category = 0, $num = 20, $start = 0) {
        $substring_procate = $this->getStringSubProductCategory($product_category);
        $arr_where_in = explode(',', $substring_procate);
        $this->db->select('products_id,code,products.image,title,products.name,products.name_alias,viewed,sprice,description,price,partners.name as partners_name, partners.name_alias as partners_name_alias');
        $this->db->join('partners', "{$this->_table}.partners_id = partners.partners_id", 'LEFT');
        $this->db->where_in('product_category_id', $arr_where_in);
        $this->db->where('products.bl_active', 1);
        $this->db->order_by('products.dt_create', 'DESC');
        $result = $this->db->get('products', $num, $start)->result();
        foreach ($result as $key => $item) {
            $salesOff = 0;
            if ($item->sprice > 0 && $item->price > 0)
                $salesOff = 100 - round($item->sprice / $item->price * 100);
            $result[$key]->salesOff = $salesOff;
        }
        return $result;
    }

    // get string product_category_id;
    function getStringSubProductCategory($parent_id = 0) {
        $str_sub = array();
        $this->db->select('product_category_id');
        $this->db->where('parent_id', $parent_id);
        $this->db->where('bl_active', 1);
        $this->db->order_by('nb_order', 'ASC');
        $resultItems = $this->db->get('product_category')->result();
        if ($resultItems) {
            foreach ($resultItems as $sub_cate_id) {
                //$str_sub[] = $sub_cate_id->product_category_id;
                $next_sub = $this->getStringSubProductCategory($sub_cate_id->product_category_id);
                if (!empty($next_sub))
                    $str_sub[] = $next_sub;
            }
        }
        $str_sub[] = $parent_id;

        return implode(',', $str_sub);
    }

    // get string product_category_id;
    function getObjectSubProductCategory($parent_id = 0) {
        $this->db->where('parent_id', $parent_id);
        $this->db->where('bl_active', 1);
        $this->db->order_by('nb_order', 'ASC');
        $resultItems = $this->db->get('product_category')->result();
        return $resultItems;
    }

    // get string product_category_id;
    function getObjectSubProductCategoryRand($parent_id = 0) {
        $this->db->where('parent_id', $parent_id);
        $this->db->where('bl_active', 1);
        $this->db->order_by('rand()');
        $resultItems = $this->db->get('product_category')->result();
        return $resultItems;
    }

    // danh muc gốc
    function getRootParent($id) {
        $arrRoot = array();
        $objectCategorys = $this->getProductCategoryId($id);
        if ($objectCategorys) {
            $root_id = $objectCategorys->parent_id;
            if ($root_id != 0) {
                $arrRoot = $this->getRootParent($root_id);
            }
            $arrRoot[] = $objectCategorys;
        }
        return $arrRoot;
    }

    // lấy danh sach sản phẩm, phân trang
    function getProducts($where = array(), $order = '', $num = 20, $start = 0) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        $this->db->where($where);
        if ($order)
            $this->db->order_by($order);
        $result = $this->db->get('products', $num, $start)->result();
        foreach ($result as $key => $item) {
            $salesOff = 0;
            if ($item->sprice > 0 && $item->price > 0)
                $salesOff = 100 - round($item->sprice / $item->price * 100);
            $result[$key]->salesOff = $salesOff;
        }
        return $result;
    }

    // tổng số mẫu tin
    function getNumProducts($where = array()) {
        $this->db->select('products_id');
        $this->db->where($where);
        return $this->db->get('products')->num_rows();
    }

    function getNumProductsFromCate($product_category) {
        $substring_procate = $this->getStringSubProductCategory($product_category);
        $arr_where_in = explode(',', $substring_procate);
        $this->db->select('products_id');
        $this->db->where_in('product_category_id', $arr_where_in);
        $this->db->where('bl_active', 1);
        return $this->db->get('products')->num_rows();
    }

    // tìm kiếm theo giá
    function getProductsFromPrice($codition) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        if (!empty($codition['price_from'])) {
            $this->db->where('price >=', $codition['price_from']);
        }
        if (!empty($codition['price_to'])) {
            $this->db->where('price <=', $codition['price_to']);
        }
        if (!empty($codition['q_search'])) {
            $this->db->like('name', $codition['q_search']);
            $this->db->or_like('title', $codition['q_search']);
            $this->db->or_like('description', $codition['q_search']);
            $this->db->or_like('meta_tags', $codition['q_search']);
        }
        $this->db->where('bl_active', 1);
        return $this->db->get('products')->result();
    }

    // tim kiếm theo tag
    function getSearchProductsFromTags($codition) {
        $this->db->select('products_id,code,image,name,name_alias,title,image,viewed,sprice,price,description');
        if (!empty($codition['alias'])) {
            $this->db->like('name', $codition['alias']);
            $this->db->or_like('title', $codition['alias']);
            $this->db->or_like('description', $codition['alias']);
            $this->db->or_like('meta_tags', $codition['alias']);
        }
        $this->db->where('bl_active', 1);
        return $this->db->get('products')->result();
    }

    // size
    function getsizeFromProduct($product_id) {
        $this->db->select('size_product.*,size.name as size_name');
        $this->db->join('size', "size_product.size_id = size.size_id", 'LEFT');
        $this->db->order_by('size.nb_order', 'ASC');
        $this->db->where('product_id', $product_id);
        return $this->db->get('size_product')->result();
    }

    // màu sắc 
    function getColorFromProduct($product_id) {
        $this->db->select('color_product.*,color.name as color_name');
        $this->db->join('color', "color_product.color_id = color.color_id", 'LEFT');
        $this->db->order_by('color.nb_order', 'ASC');
        $this->db->where('product_id', $product_id);
        return $this->db->get('color_product')->result();
    }

    //option
    function optionSize($product_id = 0) {
        $category_arr = $this->getsizeFromProduct($product_id);
        $str_opt = "";
        foreach ($category_arr as $item_opt) {
            $str_opt .="<option value='" . $item_opt->size_id . "' > " . $item_opt->size_name . "</option>";
        }
        return $str_opt;
    }

    //option color
    function optionColor($product_id = 0) {
        $category_arr = $this->getColorFromProduct($product_id);
        $str_opt = "";
        foreach ($category_arr as $item_opt) {
            $str_opt .="<option value='" . $item_opt->color_id . "' > " . $item_opt->color_name . "</option>";
        }
        return $str_opt;
    }
		function delete(){
			  $this->db->where('bl_active',1);
			  $query = $this->db->delete('products');
			  if($query){
				  return true;
			  }else{
				  return false;
			  }
	}

}